﻿@model nwrole.toolbox.sync.entity.SyncEntity

@{
    ViewBag.Title = "Show";
}

<div>
    @using (Html.BeginForm()) {
        <div class="box">
            <h2>Table</h2>
            <div class="form">
                <ul class="fields">
                    <li class="field wide">
                        @Html.HiddenFor(x => x.Id)
                        @Html.LabelFor(x => x.Id)
                        <div class="input">@Model.Id</div>
                    </li>
                    <li class="field wide">
                        @Html.LabelFor(x => x.Name)
                        <div class="input">@Model.Name</div>
                    </li>
                    <li class="field wide">
                        @Html.LabelFor(x => x.SourceTableName)
                        <div class="input">@Model.SourceTableName</div>
                    </li>
                    <li class="field wide">
                        @Html.LabelFor(x => x.ShadowTableName)
                        <div class="input">@Model.ShadowTableName</div>
                    </li>
                    <li class="field wide">
                        @Html.LabelFor(x => x.DestinationTableName)
                        @Html.EditorFor(x => x.DestinationTableName)
                    </li>
                    <li class="field wide">
                        @Html.LabelFor(x => x.TargetTableName)
                        @Html.EditorFor(x => x.TargetTableName)
                    </li>
                    <li class="field wide">
                        @Html.LabelFor(x => x.SyncEnabled)
                        <div class="input">@Model.SyncEnabled</div>
                    </li>
                    <li class="field wide">
                        @Html.LabelFor(x => x.SyncDat)
                        <div class="input">@Model.SyncDat</div>
                    </li>
                    <li class="field wide">
                        @Html.LabelFor(x => x.SortIndex)
                        @Html.EditorFor(x => x.SortIndex)
                    </li>
                    <li class="field wide">
                        @Html.LabelFor(x => x.SourceInsertedColumnName)
                        @Html.EditorFor(x => x.SourceInsertedColumnName)
                    </li>
                    <li class="field wide">
                        @Html.LabelFor(x => x.SourceUpdatedColumnName)
                        @Html.EditorFor(x => x.SourceUpdatedColumnName)
                    </li>
                    <li class="field wide">
                        @Html.LabelFor(x => x.SourceFilter)
                        @Html.EditorFor(x => x.SourceFilter)
                    </li>
                    <li class="field wide">
                        @Html.LabelFor(x => x.DestinationSyncCompleteSPName)
                        @Html.EditorFor(x => x.DestinationSyncCompleteSPName)
                    </li>
                    <li class="field wide">
                        @Html.LabelFor(x => x.SourceSyncStartSPName)
                        @Html.EditorFor(x => x.SourceSyncStartSPName)
                    </li>
                    <li class="field wide">
                        @Html.LabelFor(x => x.SourceTimestampColumnName)
                        @Html.EditorFor(x => x.SourceTimestampColumnName)
                    </li>
                    <li class="field wide">
                        @Html.LabelFor(x => x.KeepDays)
                        @Html.EditorFor(x => x.KeepDays)
                    </li>
                </ul>

                <input type="submit" value="Save" />
            </div>
        </div>
        
        <div class="box">
        </div>       
        
    }
</div>
    
<h2>Columns</h2>
<table class="box">
    
    <tr>
        <th>Column</th>
        <th>Type</th>
        <th>Length</th>
        <th>Nullable</th>
		<th>Precision</th>
		<th>Scale</th>
        <th>Enabled (<a href="" onclick="ToggleAllColumnSyncEnabled(); return false;">Toggle all</a>)</th>
    </tr>
	@{ bool even = true; }
    @foreach (var c in Model.Columns.OrderBy(c => c.ColumnName))
    {
        <tr @((even = !even) ? "class=even" : "")>
            <td class="form">
                :@c.ColumnName
            </td>        
            <td class="form">
                @c.SourceSqlTypeName
            </td>        
            <td class="form">
                @c.SourceSqlTypeMaxLength
            </td>        
            <td class="form">
                @(c.SourceSqlTypeNullableBool ? "Ja" : "Nej")
            </td>
			<td class="form">
				@c.DestSqlTypePrecision
			</td>
			<td class="form">
				@c.DestSqlTypeScale
			</td>
            <td class="form">
                <a href="" onclick="ToggleColumnSyncEnabled(this, @c.Id); return false;">@c.SyncEnabledString</a>
            </td>        

        </tr>
    }
</table>
<a href="@Url.Action("Index")">Back</a>

<script type="text/javascript">
    function ToggleColumnSyncEnabled(obj, colId) {
        $.get('@Url.Action("ToggleColumnSyncEnabled")', { id: colId, x: Math.random() }, function (result) {
            if (result.success) {
                if (result.newValue) {
                    $(obj).text('True');
                } else {
                    $(obj).text('False');
                }
            }
            else {
                alert(result.success);
            }
        });
    }

    function ToggleAllColumnSyncEnabled() {
        $.get('@Url.Action("ToggleAllColumnSyncEnabled")', { id: @Model.Id, x: Math.random() }, function (result) {
            if (result.success) {
                window.location = '@Url.Action("Show", new { id = Model.Id })';
            }
            else {
                alert(result.success);
            }
        });
    }
</script>